<template>
  <el-dialog
    title="新增员工"
    :visible.sync="dialogVisible"
    width="50%"
    @close="closeEvent"
  >
    <el-form ref="form" :model="form" label-width="120px" :rules="rules">
      <el-form-item prop="username" label="姓名">
        <el-input v-model="form.username" placeholder="2-4个字符" />
      </el-form-item>
      <el-form-item prop="mobile" label="手机">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item prop="timeOfEntry" label="入职时间">
        <el-date-picker
          v-model="form.timeOfEntry"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item prop="formOfEmployment" label="聘用形式">
        <el-select v-model="form.formOfEmployment" placeholder="请选择">
          <el-option
            v-for="item in employeesData.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="workNumber" label="工号">
        <el-input v-model="form.workNumber" placeholder="1-20个字符" />
      </el-form-item>
      <el-form-item prop="departmentName" label="部门">
        <el-input
          v-model="form.departmentName"
          readonly
          placeholder="1-20个字符"
          @focus="focusEvent"
        />
        <el-tree
          v-if="showTree"
          v-loading="loading"
          :data="departmentList"
          :props="{ label: 'name' }"
          @node-click="nodeClick"
        />
      </el-form-item>
      <el-form-item prop="correctionTime" label="转正时间">
        <el-date-picker
          v-model="form.correctionTime"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
    </el-form>
    <template #footer style="text-align: center">
      <el-button @click="closeEvent">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import employeesData from '@/api/constant/employees.js'
import { companyDepartment } from '@/api/department.js'
import { changeData } from '@/utils/index.js'
import { checkPhone } from '@/utils/validate.js'
import { sysUserPost } from '@/api/employees.js'
export default {
  name: '',
  data() {
    return {
      loading: false,
      showTree: false,
      // 导入枚举数据并赋值到dv-, ata
      employeesData,
      form: {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        timeOfEntry: '', //	string	非必须		入职时间
        correctionTime: '' //	string	非必须		转正时间
      },
      departmentList: [],
      dialogVisible: false,
      rules: {
        username: [
          { required: true, message: '必填', trigger: 'change' },
          {
            min: 2,
            max: 4,
            message: '请输入2-4个字符',
            trigger: 'change'
          }
        ],
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              checkPhone(value)
                ? callback()
                : callback(new Error('手机号不正确'))
            },
            trigger: 'change'
          }
        ],
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        workNumber: [{ required: true, message: '必填', trigger: 'change' }],
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        timeOfEntry: [{ required: true, message: '必填', trigger: 'change' }],
        correctionTime: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  computed: {},
  created() {},
  methods: {
    async focusEvent() {
      this.showTree = true
      if (this.departmentList.length === 0) {
        this.loading = true
        const res = await companyDepartment()
        this.loading = false
        this.departmentList = changeData(res.data.depts, '')
      }
      // console.log(res)
    },
    nodeClick(row) {
      this.form.departmentName = row.name
      this.showTree = false
    },

    submit() {
      this.$refs.form.validate(async(res) => {
        if (res) {
          await sysUserPost(this.form)
          this.$message.success('新增成功')
          this.$emit('getData')
          this.closeEvent()
        }
      })
    },
    closeEvent() {
      this.dialogVisible = false
      this.$refs.form.resetFields()
      this.form = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
    }
  }
}
</script>
<style lang="less" scoped></style>
